package com.spatio.serving.basic.service.template.server.controller;

import com.spatio.serving.common.lib.common.entity.ApiResponse;
import com.spatio.serving.spring.boot.starter.core.context.SpatioLoginContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.concurrent.Executors;
import java.util.stream.IntStream;

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

/*    @Resource
    private ApplicationConfig applicationConfig;*/

    @GetMapping("/stream")
    public SseEmitter streamData() {
        SseEmitter emitter = new SseEmitter();
        Executors.newSingleThreadExecutor().execute(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    emitter.send("{\"message\":\"" + i + "\"}", MediaType.APPLICATION_JSON);
                    Thread.sleep(100); // 模拟延迟
                }
                emitter.complete();
            } catch (Exception e) {
                emitter.completeWithError(e);
            }
        });
        return emitter;
        }

    @GetMapping("/test")
    public ApiResponse test() {
        System.out.println("voice_access_key_id: " /*+ applicationConfig.getApplicationValue("voice_access_key_id")*/);
        //System.out.println(applicationConfig.getApplicationValue("spring.profiles.active"));
        return ApiResponse.success(SpatioLoginContext.generateTraceId());
    }

    public static void main(String[] args) throws Exception {
        int depth = 4;
        int length = 1 << depth;
        int[] binaryHeap = IntStream.rangeClosed(0, length-1).toArray();
        traverse(binaryHeap);
    }
    static void traverse(int[] binaryHeap) {
        int compare = -(binaryHeap.length);
        int index = 1;
        traverse(index, compare, binaryHeap);
        System.out.println();
    }
    static void traverse(int index, int compare, int[] array) {
        if ((index & compare)== 0) {
            System.out.print(array[index] + " ");
            index <<= 1;
            traverse(index, compare, array);
            index ^= 1;
            traverse(index, compare, array);
        }
    }

}
